package net.pzc.filechooser;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class ToggleButtonGroupPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private DefaultComboBoxModel model = null;
	private ButtonGroup btnGroup = null;
	private List<JToggleButton> btnList = null;
//	private JToggleButton preBtn = null;

	private List<ToggleItemMouseListener> _itemClickListeners = null;

	public ToggleButtonGroupPanel() {
		this.model = new DefaultComboBoxModel();
		this.model.addListDataListener(new _Li(this));
		this.btnGroup = new ButtonGroup();
		this.btnList = new ArrayList<JToggleButton>();
		this._itemClickListeners = new ArrayList<ToggleItemMouseListener>();
	}

	public DefaultComboBoxModel getModel() {
		return model;
	}

	public void addToggleItemClickListener(ToggleItemMouseListener listener) {
		this._itemClickListeners.add(listener);
	}

	private void mouseIn(JToggleButton btn) {
		if (btn.isEnabled() && btn.isSelected() == false) {
			btn.setBorderPainted(true);
		}
	}

	private void mouseOut(JToggleButton btn) {
		if (btn.isEnabled() && btn.isSelected() == false) {
			btn.setBorderPainted(false);
		}
	}

	@Override
	public void setEnabled(boolean flag) {
		super.setEnabled(flag);
		for (JToggleButton btn : btnList) {
			btn.setEnabled(flag);
		}
	}

	private class _Li implements ListDataListener {

		private ToggleButtonGroupPanel self = null;

		public _Li(ToggleButtonGroupPanel self) {
			this.self = self;
		}

		public void contentsChanged(ListDataEvent event) {
			if (model.getSelectedItem() != null) {
				JToggleButton btn = btnList.get(model.getIndexOf(model.getSelectedItem()));
				btn.setSelected(true);
			} else {
//				btnGroup.clearSelection();
				btnGroup.getSelection().setSelected(false);
			}
		}

		public void intervalAdded(ListDataEvent event) {
			final FileItem fi = (FileItem) model.getElementAt(event.getIndex0());
			final JToggleButton btn = new FFCToggleButton();
			btn.setLayout(new GridLayout(2, 1));

			JLabel lblIcon = new JLabel(new ImageIcon(fi.getIcon()));
			lblIcon.setVerticalAlignment(SwingConstants.BOTTOM);
			btn.add(lblIcon);

			JLabel lblText = new JLabel(fi.getSysDisplayName());
			lblText.setVerticalAlignment(SwingConstants.TOP);
			lblText.setHorizontalAlignment(SwingConstants.CENTER);
			btn.add(lblText);

			btn.setToolTipText(fi.getSysDisplayName());
			btn.setBorderPainted(false);
			btnGroup.add(btn);
			btnList.add(btn);
			self.add(btn);

			btn.addChangeListener(new ChangeListener() {

				public void stateChanged(ChangeEvent event) {
					if (btn.isEnabled()) {
						btn.setBorderPainted(btn.isSelected());
					}
				}

			});

			btn.addMouseMotionListener(new MouseMotionListener() {

				public void mouseMoved(MouseEvent event) {
					mouseIn(btn);
				}

				public void mouseDragged(MouseEvent e) {
					
				}

			});

			btn.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseEntered(MouseEvent event) {
					mouseIn(btn);
				}

				@Override
				public void mouseExited(MouseEvent event) {
					mouseOut(btn);
				}

				@Override
				public void mouseClicked(MouseEvent event) {
//					if (btn.isEnabled()) {
//						if (_itemClickListeners.size() > 0) {
//							for (int i = _itemClickListeners.size() - 1; i >= 0; i--) {
//								_itemClickListeners.get(i).onClick(btn, fi);
//							}
//						}
//					}
				}

				@Override
				public void mousePressed(MouseEvent event) {
					if (btn.isEnabled()) {
//					if (btn.isEnabled() && preBtn != btn) {
//						if (preBtn != null) {
//							preBtn.setSelected(false);
//						}
//						preBtn = btn;
						if (_itemClickListeners.size() > 0) {
							for (int i = _itemClickListeners.size() - 1; i >= 0; i--) {
								_itemClickListeners.get(i).onMousePressed(btn, fi);
							}
						}
					}
				}
			});
		}

		public void intervalRemoved(ListDataEvent event) {
			// Do nothing
		}

	}

}
